package al24swapPairs;


class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class SwapPairs24 {

    public static ListNode swapPairs(ListNode head) {

        if (head==null || head.next == null){
            return head;
        }

        ListNode dummy = new ListNode(0, head);
        ListNode node1 = head;
        ListNode node2 = head.next;
        ListNode node0 = dummy;
        while (node2!=null){
            node1.next = node2.next;
            node0.next = node2;
            node2.next = node1;

            node0 = node1;
            node1 = node1.next;
            if (node1==null){
                break;
            }
            node2 = node1.next;
        }


        return dummy.next;
    }

    public static void printList(ListNode head) {
        ListNode current = head;
        while (current != null) {
            System.out.print(current.val + " ");
            current = current.next;
        }
        System.out.println();
    }

    // 主方法用于测试
    public static void main(String[] args) {
        // 创建两个有序链表
        ListNode l1 = new ListNode(0);
        l1.next = new ListNode(1);
/*        l1.next.next = new ListNode(2);
        l1.next.next.next = new ListNode(3);
        l1.next.next.next.next = new ListNode(4);
        l1.next.next.next.next.next = new ListNode(5);*/
/*        l1.next.next.next.next.next.next = new ListNode(6);*/

        // 打印原链表
        System.out.println("List 1:");
        printList(l1);


        // 合并两个链表
        ListNode mergedList = swapPairs(l1);

        // 打印合并后的链表
        System.out.println("Swaped List:");
        printList(mergedList);
    }
}
